﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Configuration;

namespace DuJianliang.ApplicationPlatform.Common
{
    public static class EmailSender
    {
        public static void Send(List<string> toAddressList)
        {
            if (toAddressList == null || toAddressList.Count == 0)
            {
                return;
            }

            string settings_email = ConfigurationManager.AppSettings["email"];
            string settings_fromAddress = ConfigurationManager.AppSettings["email.fromAddress"];
            string settings_fromName = ConfigurationManager.AppSettings["email.fromName"];
            string settings_subject = ConfigurationManager.AppSettings["email.subject"];
            string settings_mailContent = ConfigurationManager.AppSettings["email.mailContent"];
            string settings_username = ConfigurationManager.AppSettings["email.username"];
            string settings_password = ConfigurationManager.AppSettings["email.password"];
            string settings_port = ConfigurationManager.AppSettings["email.port"];
            string settings_host = ConfigurationManager.AppSettings["email.host"];

            if (string.IsNullOrWhiteSpace(settings_email) || !settings_email.ToLower().Equals("true")
                || string.IsNullOrWhiteSpace(settings_fromAddress)
                || string.IsNullOrWhiteSpace(settings_fromName)
                || string.IsNullOrWhiteSpace(settings_subject)
                || string.IsNullOrWhiteSpace(settings_mailContent)
                || string.IsNullOrWhiteSpace(settings_username)
                || string.IsNullOrWhiteSpace(settings_password)
                || string.IsNullOrWhiteSpace(settings_port)
                || string.IsNullOrWhiteSpace(settings_host))
            {
                return;
            }            

            try
            {
                MailMessage msg = new MailMessage();

                // 添加收件人
                foreach (string toAddress in toAddressList)
                {
                    msg.To.Add(toAddress);
                }

                // 发件人信息
                msg.From = new MailAddress(settings_fromAddress, settings_fromName, System.Text.Encoding.UTF8);
                msg.Subject = settings_subject;                              // 邮件标题
                msg.SubjectEncoding = System.Text.Encoding.UTF8;    // 标题编码
                msg.Body = settings_mailContent;                             // 邮件主体
                msg.BodyEncoding = System.Text.Encoding.UTF8;
                msg.IsBodyHtml = true;                              // 是否HTML
                msg.Priority = MailPriority.High;                   // 优先级

                SmtpClient client = new SmtpClient();
                // 设置邮箱和密码 
                client.Credentials = new System.Net.NetworkCredential(settings_username, settings_password);
                client.Port = int.Parse(settings_port);
                client.Host = settings_host;
                //client.EnableSsl = true;
                object userState = msg;

                client.Send(msg);
            }
            catch (Exception ex)
            {
                Logger.Write(null, 1, "email sender", "exception", ex);
            } 
        }
    }
}
